﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YouErYuanModels;

namespace YouErYuanBLL
{
    public class VideoSettingBLL
    {
        /// <summary>
        /// 分页查询幼儿园班级记录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="youErYuanName">幼儿园班级名称</param>
        /// <param name="total">条件查询总记录数</param>
        /// <returns></returns>
        public object VideoSettingPageList(int pageIndex, int pageSize, string youErYuanName, out int total)
        {
            string where = "";
            if (string.IsNullOrEmpty(youErYuanName) == false)
            {
                youErYuanName = SqlFilter.Filter(youErYuanName);
                where = " schoolid = '" + youErYuanName + "'";
            }
            DataTable dt = SQLHepler.GetPaging("[deviceSerialNo] desc", "[V_Video]", where, pageIndex, pageSize, out total);
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(new DataColumn("checkVideo"));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var item = dt.Rows[i];
                    string checkVideoURL = item["videoURL"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["videoURL"]);
                    if (string.IsNullOrEmpty(checkVideoURL) || string.IsNullOrWhiteSpace(checkVideoURL))
                    {
                        //地址未获取到
                        dt.Rows[i]["checkVideo"] = "0";
                    }
                    else
                    {
                        dt.Rows[i]["checkVideo"] = "1";
                    }
                }
            }

            return dt;
        }

        /// <summary>
        /// 视频设备信息
        /// </summary>
        /// <param name="videId">视频设备id</param>
        /// <returns></returns>
        public object VideoInfo(string videId)
        {
            string sql = "SELECT [id],[videoURL],[coverImgURL],[classId],[videoTitle],[state],[schoolId],[deviceSerialNo],[className] FROM [dbo].[V_Video] where [id]=@vid";
            DataTable dt = SQLHepler.GetDataTable(sql, new SqlParameter("@vid", videId));
            return dt;
        }

        /// <summary>
        /// 根据主键获取视频直播信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public VideoLive GetVideoLive(string videoId)
        {
            VideoLive model = new VideoLive();
            string sql = "SELECT [id],[videoURL],[coverImgURL],[classId],[videoTitle],[schoolId],[deviceSerialNo] FROM [dbo].[T_VideoLive] where [id]=@id";
            DataTable dt = SQLHepler.GetDataTable(sql, new SqlParameter("@id", videoId));
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var item = dt.Rows[i];
                    model.id = Convert.ToString(item["id"]);
                    model.videoURL = (item["videoURL"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["videoURL"]);
                    model.coverImgURL = (item["coverImgURL"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["coverImgURL"]);
                    model.classId = (item["classId"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["classId"]);
                    model.videoTitle = (item["videoTitle"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["videoTitle"]);
                    model.schoolId = (item["schoolId"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["schoolId"]);
                    model.deviceSerialNo = (item["deviceSerialNo"]).GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["deviceSerialNo"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 修改视频设备信息
        /// </summary>
        /// <param name="videId">视频设备id</param>
        /// <returns></returns>
        public object UpdateVideo(VideoLive video)
        {
            int count = 0;
            if (video != null)
            {
                string sql = "update [dbo].[T_VideoLive] set [deviceSerialNo]=@deviceSerialNo,[classId]=@classId,[videoTitle]=@videoTitle,[videoURL]=@videoURL,[coverImgURL]=@coverImgURL,[state]=@state where [id]=@id";
                List<SqlParameter> paramesList = new List<SqlParameter>();
                object videoTitle_parame = video.videoTitle;
                object coverImgURL_parame = video.coverImgURL;
                object classId_parame = video.classId;
                object videoURL_parame = video.videoURL;
                object state_parame = video.state;

                if (string.IsNullOrEmpty(video.classId) || string.IsNullOrWhiteSpace(video.classId))
                {
                    classId_parame = DBNull.Value;
                }
                if (string.IsNullOrEmpty(video.videoTitle) || string.IsNullOrWhiteSpace(video.videoTitle))
                {
                    videoTitle_parame = DBNull.Value;
                }
                if (string.IsNullOrEmpty(video.coverImgURL) || string.IsNullOrWhiteSpace(video.coverImgURL))
                {
                    coverImgURL_parame = DBNull.Value;
                }
                if (string.IsNullOrEmpty(video.videoURL) || string.IsNullOrWhiteSpace(video.videoURL))
                {
                    videoURL_parame = DBNull.Value;
                }
                if (string.IsNullOrEmpty(video.state) || string.IsNullOrWhiteSpace(video.state))
                {
                    state_parame = "0";
                }
                else
                {
                    string[] stateArray = { "1", "0" };
                    if (stateArray.Contains(video.state) == false)
                    {
                        state_parame = "0";
                    }
                }
                paramesList.Add(new SqlParameter("@id", video.id));
                paramesList.Add(new SqlParameter("@classId", classId_parame));
                paramesList.Add(new SqlParameter("@videoTitle", videoTitle_parame));
                paramesList.Add(new SqlParameter("@videoURL", videoURL_parame));
                paramesList.Add(new SqlParameter("@coverImgURL", coverImgURL_parame));
                paramesList.Add(new SqlParameter("@deviceSerialNo", video.deviceSerialNo.Trim()));
                paramesList.Add(new SqlParameter("@state", state_parame));
                SqlParameter[] parames = paramesList.ToArray<SqlParameter>();
                count = SQLHepler.ExecuteSql(sql, parames);
            }
            return count;
        }

        /// <summary>
        /// 班级查询
        /// </summary>
        /// <param name="schoolid"></param>
        /// <returns></returns>
        public object SelectClassForSchoolid(string schoolid)
        {
            string sql = "select id,className from dbo.T_Class where schoolid=@schoolid";
            SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@schoolid", schoolid) };
            DataTable dt = SQLHepler.GetDataTable(sql, parameter);
            return dt;
        }

        /// <summary>
        /// 插入一条摄像头
        /// </summary>
        /// <param name="vl"></param>
        /// <returns></returns>
        public int InsertVideo(VideoLive vl)
        {

            List<SqlParameter> paramelist = new List<SqlParameter>();
            string classId_sql = string.Empty;
            string videoTitle_sql = string.Empty;
            string corverImgURL_sql = string.Empty;//视频封面图片
            string videoURL_sql = string.Empty;//视频直播地址

            string classId_parame = string.Empty;
            string videoTitle_parame = string.Empty;
            string corverImgURL_parame = string.Empty;
            string videoURL_parame = string.Empty;

            if (string.IsNullOrEmpty(vl.classId) == false && string.IsNullOrWhiteSpace(vl.classId) == false)
            {
                classId_sql = ",classId";
                classId_parame = ",@classId";
                paramelist.Add(new SqlParameter("@classId", vl.classId));
            }
            if (string.IsNullOrEmpty(vl.videoTitle) == false && string.IsNullOrWhiteSpace(vl.videoTitle) == false)
            {
                videoTitle_sql = ",videoTitle";
                videoTitle_parame = ",@videoTitle";
                paramelist.Add(new SqlParameter("@videoTitle", vl.videoTitle));
            }
            if (string.IsNullOrEmpty(vl.coverImgURL) == false && string.IsNullOrWhiteSpace(vl.coverImgURL) == false)
            {
                corverImgURL_sql = ",[coverImgURL]";
                corverImgURL_parame = ",@coverImgURL";
                paramelist.Add(new SqlParameter("@coverImgURL", vl.coverImgURL));
            }
            if (string.IsNullOrEmpty(vl.videoURL) == false && string.IsNullOrWhiteSpace(vl.videoURL) == false)
            {
                videoURL_sql = ",[videoURL]";
                videoURL_parame = ",@videoURL";
                paramelist.Add(new SqlParameter("@videoURL", vl.videoURL));
            }
            string viewState = vl.state;
            if (string.IsNullOrEmpty(vl.state) || string.IsNullOrWhiteSpace(vl.state))
            {
                viewState = "0";
            }
            else
            {
                string[] stateArray = { "1", "0" };
                if (stateArray.Contains(vl.state) == false)
                {
                    viewState = "0";
                }
            }
            string sql = "insert into T_VideoLive (id" + classId_sql + videoTitle_sql + videoURL_sql + corverImgURL_sql + ",schoolId,deviceSerialNo,state) values (newid()" + classId_parame + videoTitle_parame + videoURL_parame + corverImgURL_parame + ",@schoolId,@deviceSerialNo,@state)";
            paramelist.Add(new SqlParameter("@schoolId", vl.schoolId));
            paramelist.Add(new SqlParameter("@deviceSerialNo", vl.deviceSerialNo));
            paramelist.Add(new SqlParameter("@state", viewState));
            SqlParameter[] parameter = paramelist.ToArray<SqlParameter>();
            int count = SQLHepler.ExecuteSql(sql, parameter);
            return count;
        }

        /// <summary>
        /// 删除一个摄像头
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public int DeleteVideo(string videoId)
        {
            string sql = "delete from [dbo].[T_VideoLive] where [id]=@videoId";
            return SQLHepler.ExecuteSql(sql, new SqlParameter("@videoId", videoId)); ;
        }

        /// <summary>
        /// 检查摄像头序列号是否存在，1=存在，0=不存在
        /// </summary>
        /// <param name="serialNo">摄像头序列号</param>
        /// <returns></returns>
        public int CheckDeviceSerialNo(string serialNo, string schoolId)
        {
            string sql = " SELECT COUNT(1) FROM [dbo].[T_VideoLive] where [deviceSerialNo]=@deviceSerialNo and [schoolId]=@schoolId";
            SqlParameter[] parames =
            {
                new SqlParameter("@deviceSerialNo", serialNo),
                new SqlParameter("@schoolId", schoolId)
            };
            object obj = SQLHepler.GetSingleValue(sql, parames);
            int count = Convert.ToInt32(obj);
            if (count > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }



    }
}
